﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.Windows.Forms;
namespace SPMigrationOracleUtility
{

    class HelperClass
    {
        public string ErrorMsg
        { get; set; }
        public string Uname
        { get; set; }
        public string pwd
        { get; set; }
        public string Newpwd
        { get; set; }
        public string SiteUrl
        { get; set; }

        public int Itemid
        { get; set; }

        public ListItemCollection oLoginDetails
        {
            get;set;
        }

        public HelperClass(string userName, string Password)
        {
            //MessageBox.Show("try to get Config"); 
            SiteUrl = Convert.ToString(SPMigrationOracleUtility.Appl.Default.SiteURL);
            Uname = userName;
            pwd = Password;
            try
            {
                //MessageBox.Show("Access Site");
               ClientContext ctx = new ClientContext(SiteUrl);
                Web oweb = ctx.Web;
                List olist = ctx.Web.Lists.GetByTitle("Admin Credentials");
                // Load only the list variable and execute the query
                ctx.Load(olist);
                ctx.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View></View>";
                oLoginDetails = olist.GetItems(camlQuery);
                ctx.Load(oLoginDetails);
                ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("HelperClass Uname Password:" + ex.Message);  
            }
        }
        public HelperClass(string newpassword)
        {
            SiteUrl = Convert.ToString(SPMigrationOracleUtility.Appl.Default.SiteURL);
            Newpwd   = newpassword;
            try
            {
                ClientContext ctx = new ClientContext(SiteUrl);
                Web oweb = ctx.Web;
                List olist = ctx.Web.Lists.GetByTitle("Admin Credentials");
                // Load only the list variable and execute the query
                ctx.Load(olist);
                ctx.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View></View>";
                oLoginDetails = olist.GetItems(camlQuery);
                ctx.Load(oLoginDetails);
                ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("HelperClass ::" + ex.Message);  
            }
        }

        public bool CheckUserExist()
        {
            bool rval = false;
            try
            {
                foreach (ListItem oitm in oLoginDetails)
                {
                    if (Convert.ToString(oitm["Title"]) == Uname && Convert.ToString(oitm["Password"]) == pwd)
                    {
                        rval = true;
                        Itemid = oitm.Id; 
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("CheckUserExist ::" + ex.Message);  
            }
           
            return rval;
        }

        public bool changePassword()
        {
            bool rval = false;
            try
            {
                ClientContext ctx = new ClientContext(SiteUrl);
                Web oweb = ctx.Web;
                List olist1 = ctx.Web.Lists.GetByTitle("Admin Credentials");
                // Load only the list variable and execute the query
                ctx.Load(olist1);
                ctx.ExecuteQuery();
                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem oListItem = olist1.GetItemById(Itemid); 
                oListItem["Password"] = Newpwd;
                oListItem.Update();
                ctx.ExecuteQuery();
                if (oListItem["Password"] != null)
                {
                    rval = true;
                }
            }
            catch (Exception ex)
            {
                ErrorMsg = ex.Message;
                MessageBox.Show("changePassword ::" + ex.Message);  
            }
            return rval;
        }

       

    }
}
